package com.feisuanyz.lyweb01.service.impl;

import com.feisuanyz.lyweb01.dao.NewsMapper;
import com.feisuanyz.lyweb01.pojo.domain.NewsDO;
import com.feisuanyz.lyweb01.pojo.dto.NewsDTO;
import com.feisuanyz.lyweb01.pojo.query.NewsQuery;
import com.feisuanyz.lyweb01.pojo.vo.PageResult;
import com.feisuanyz.lyweb01.service.NewsService;
import java.lang.Boolean;
import java.lang.Override;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.feisuanyz.lyweb01.exception.BusinessException;
import java.util.List;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import java.util.Map;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.feisuanyz.lyweb01.constant.ResultCodeConstant;

/**
 * 新联资讯的实现
 *
 * @author deng
 * @date 2025-02-26 11:57:37
 */
@Service
public class NewsServiceImpl implements NewsService {

    @Autowired
    private NewsMapper newsMapper;

    @Override
    public PageResult<NewsDO> getNewsList(NewsQuery newsQuery) {
        try {
            IPage<NewsDO> page = new Page<>(newsQuery.getPageIndex(), newsQuery.getPageSize());
            IPage<NewsDO> newsPage = newsMapper.selectPage(page, Wrappers.emptyWrapper());
            PageResult<NewsDO> pageResult = new PageResult<>(newsPage);
            return pageResult;
        } catch (Exception e) {
            e.printStackTrace();
            throw new BusinessException(ResultCodeConstant.CODE_999999, ResultCodeConstant.CODE_999999_MSG);
        }
    }

    @Override
    public NewsDO getNewsInfo(NewsQuery newsQuery) {
        try {
            NewsDO newsDO = newsMapper.selectById(newsQuery.getNewsId());
            if (newsDO != null) {
                return newsDO;
            } else {
                throw new BusinessException(ResultCodeConstant.CODE_000001, ResultCodeConstant.CODE_000001_MSG);
            }
        } catch (Exception e) {
            e.printStackTrace();
            throw new BusinessException(ResultCodeConstant.CODE_999999, ResultCodeConstant.CODE_999999_MSG);
        }
    }

    @Override
    public PageResult<NewsDO> searchNews(NewsQuery newsQuery) {
        try {
            QueryWrapper<NewsDO> queryWrapper = Wrappers.<NewsDO>query().like("news_title", newsQuery.getKeyword());
            IPage<NewsDO> page = new Page<>(newsQuery.getPageIndex(), newsQuery.getPageSize());
            IPage<NewsDO> newsPage = newsMapper.selectPage(page, queryWrapper);
            PageResult<NewsDO> pageResult = new PageResult<>(newsPage);
            return pageResult;
        } catch (Exception e) {
            e.printStackTrace();
            throw new BusinessException(ResultCodeConstant.CODE_999999, ResultCodeConstant.CODE_999999_MSG);
        }
    }

    @Override
    public Boolean addNews(NewsDTO newsDTO) {
        try {
            NewsDO newsDO = new NewsDO();
            newsDO.setNewsTitle(newsDTO.getNewsTitle());
            newsDO.setNewsContent(newsDTO.getNewsContent());
            newsDO.setNewsImageUrl(newsDTO.getNewsImageUrl());
            int result = newsMapper.insert(newsDO);
            return result > 0;
        } catch (Exception e) {
            e.printStackTrace();
            throw new BusinessException(ResultCodeConstant.CODE_999999, ResultCodeConstant.CODE_999999_MSG);
        }
    }

    @Override
    public Boolean updateNews(NewsDTO newsDTO) {
        try {
            NewsDO newsDO = new NewsDO();
            newsDO.setNewsId(newsDTO.getNewsId());
            newsDO.setNewsTitle(newsDTO.getNewsTitle());
            newsDO.setNewsContent(newsDTO.getNewsContent());
            newsDO.setNewsImageUrl(newsDTO.getNewsImageUrl());
            int result = newsMapper.updateById(newsDO);
            return result > 0;
        } catch (Exception e) {
            e.printStackTrace();
            throw new BusinessException(ResultCodeConstant.CODE_999999, ResultCodeConstant.CODE_999999_MSG);
        }
    }

    @Override
    public Boolean deleteNews(NewsQuery newsQuery) {
        try {
            int result = newsMapper.deleteById(newsQuery.getNewsId());
            return result > 0;
        } catch (Exception e) {
            e.printStackTrace();
            throw new BusinessException(ResultCodeConstant.CODE_999999, ResultCodeConstant.CODE_999999_MSG);
        }
    }
}
